package org.roulette.controller;

import org.roulette.model.Bet;
import org.roulette.model.Player;
import org.roulette.model.Table;
import org.roulette.model.Field;
import org.roulette.model.Game;
import java.util.Random;
public final class Updater {
	Game game;

	public Updater(){
		game = Game.getInstance();	
	}

	public void updateTimer(int counter){
		game.setCounter(counter);
	}

	public void updateGame(){
		Random r = new Random();
		for(Table t: Game.getTableList()){
			t.setResult(r.nextInt(35) + 1);
		}
		betReturns();
	}

	public void betReturns(){
		for(Table t: Game.getTableList()){
			for(Field f: t.getBetOptions()){
				for(Bet b: f.getBets()){			
					Player p = t.getPlayerByName(b.getName());
					if(p != null){
						String rouletteResultName = t.getBetOptions().get(t.getResult()).getName();
						if(t.getResult() == f.getIndex()){
							p.setStack((int) (p.getStack() + b.getBet() * 36) );
						}
						else{
							switch (f.getName()){
							case "even" :
								if((t.getResult() % 2) == 0){
									p.setStack((int) (p.getStack() + b.getBet() * 2) );
								}
								break;
							case "oneven" :{
								if((t.getResult() % 2) == 1)
									p.setStack((int) (p.getStack() + b.getBet() * 2) );
							}
							break;
							case "eersteTwaalf" :{
								if((t.getResult() < 13  && t.getResult() > 0))
									p.setStack((int) (p.getStack() + b.getBet() * 3) );
							}
							break;
							case "tweedeTwaalf" :{
								if((t.getResult() < 25  && t.getResult() > 11))
									p.setStack((int) (p.getStack() + b.getBet() * 3) );
							}
							break;
							case "derdeTwaalf" :{
								if((t.getResult() < 37 && t.getResult() > 23))
									p.setStack((int) (p.getStack() + b.getBet() * 2) );
							}
							break;
							case "eersteAchttien" :{
								if((t.getResult() < 19 && t.getResult() > 0))
									p.setStack((int) (p.getStack() + b.getBet() * 2) );
							}
							break;
							case "tweedeAchttien" :{
								if((t.getResult() < 37 && t.getResult() > 18))
									p.setStack((int) (p.getStack() + b.getBet() * 2) );
							}
							break;
							case "theRed" :{
								if(rouletteResultName == "red")
									p.setStack((int) (p.getStack() + b.getBet() * 2) );
							}
							break;
							case "theBlack" :{
								if(rouletteResultName == "black")
									p.setStack((int) (p.getStack() + b.getBet() * 2) );			
								break;
							}
							}
						}

					}
				}
				f.removeBets();
			}
		}
	}
}